package com.xindaoapp.happypet.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.LoginManager;

/**
 * 注册页面
 * 
 * @author chenghao
 * 
 */
public class RegisterActivity extends BaseFragment implements OnClickListener {
    private EditText vUsername;
    private EditText vPassword;
    private EditText vPassword2;
    private EditText vEmail;
    private View vRegisterButton;
    private CheckBox vAgreement;

    private LoginManager mLoginManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_register, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mLoginManager = new LoginManager(getActivity());
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        vUsername = (EditText) view.findViewById(R.id.register_username);
        vPassword = (EditText) view.findViewById(R.id.register_paw1);
        vPassword2 = (EditText) view.findViewById(R.id.register_paw2);
        vEmail = (EditText) view.findViewById(R.id.register_mail);
        vRegisterButton = view.findViewById(R.id.register);
        vAgreement = (CheckBox) view.findViewById(R.id.xieyi);
    }

    @Override
    protected void initEvents(View view) {
        super.initEvents(view);
        vRegisterButton.setOnClickListener(this);
    }

    @Override
    protected String getTitleName() {
        return "注册";
    }

    @Override
    protected boolean showRightView() {
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register:
                String username = vUsername.getText().toString().trim();
                String password1 = vPassword.getText().toString().trim();
                String password2 = vPassword2.getText().toString().trim();
                String email = vEmail.getText().toString().trim();
                if (checkInput(username, password1, password2, email)) {
                    mLoginManager.register(username, password1, email);
                }
                break;
            default:
                break;
        }
    }

    private boolean checkInput(String username, String password1, String password2, String email) {
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(mActivity, "请输入用户名", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(password1) || TextUtils.isEmpty(password2)) {
            Toast.makeText(mActivity, "密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!password1.equals(password2)) {
            Toast.makeText(mActivity, "密码不一致", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!password1.matches("^[0-9a-zA-Z]{6,22}$")) {
            Toast.makeText(mActivity, "请输入正确的密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(email)) {
            Toast.makeText(mActivity, "邮箱不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!email
                .matches("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")) {
            Toast.makeText(mActivity, "邮箱格式不正确", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!vAgreement.isChecked()) {
            Toast.makeText(mActivity, "请阅读并同意尾巴圈协议", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
